package com.dragon.loan.service;

import com.dragon.loan.entity.AcIdentityMapperMd5;
import com.dragon.loan.repository.AcIdentityMapperMd5Mapper;
import com.fintech.common.util.MD5Utils;
import com.marvels.common.utils.MarvelsLogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AcIdentityMapperMd5ServiceImpl implements AcIdentityMapperMd5Service{
	
	@Autowired
	private AcIdentityMapperMd5Mapper acIdentityMapperMd5Mapper;
	
	@Override
	public void insertByIdCard(String idCard) {
		String idCardMD5 = MD5Utils.getMD5(idCard);
		AcIdentityMapperMd5 acIdentityMapperMd5 = new AcIdentityMapperMd5();
		acIdentityMapperMd5.setIdCard(idCard);
		acIdentityMapperMd5.setIdCardMd5(idCardMD5);
		try {
			acIdentityMapperMd5Mapper.insertSelective(acIdentityMapperMd5);
		} catch (Exception e) {
		}
	}

	@Override
	public boolean isHasIdCardByIdCardMD5(String idCardMD5) {
		Long count = acIdentityMapperMd5Mapper.countByIdCardMD5(idCardMD5);
		return count != null && count > 0L;
	}

	@Override
	public List<String> getByIdCardMd5(String idCardMd5) {
		return acIdentityMapperMd5Mapper.getByIdCardMd5(idCardMd5);
	}

	@Override
	public void insertByIdCardAndMobile(String idCard, String mobileNo) {
		AcIdentityMapperMd5 acIdentityMapperMd5 = new AcIdentityMapperMd5();
		
		if(StringUtils.isNotBlank(idCard)) {
			String idCardMD5 = MD5Utils.getMD5(idCard);
			acIdentityMapperMd5.setIdCard(idCard);
			acIdentityMapperMd5.setIdCardMd5(idCardMD5);
		
		}
		
		if(StringUtils.isNotBlank(mobileNo)) {
			String mobileMD5 = MD5Utils.getMD5(mobileNo);
			acIdentityMapperMd5.setMobile(mobileNo);
			acIdentityMapperMd5.setMobileMd5(mobileMD5);
		}

		
		
		try {
			acIdentityMapperMd5Mapper.insertSelective(acIdentityMapperMd5);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("插入实名认证信息MD5表失败,mobileNo="+mobileNo+",idCard="+idCard, e);
		}
		
	}

	@Override
	public AcIdentityMapperMd5 getMobileMd5InfoByMobileMd5(String mobileMd5) throws Exception {
		// TODO Auto-generated method stub
		return acIdentityMapperMd5Mapper.getMobileMd5InfoByMobileMd5(mobileMd5);
	}
	
	/**
	 * 根据Mobile明文查询加密用户信息
	 * @param mobile
	 * @return
	 * @throws Exception
	 */
	@Override
	public AcIdentityMapperMd5 getMobileMd5InfoByMobile(String mobile)throws Exception{
		return acIdentityMapperMd5Mapper.getMobileMd5InfoByMobile(mobile);
	}

	@Override
	public int insertAcIdentityMapperMd5Info(AcIdentityMapperMd5 acIdentityMapperMd5) throws Exception {
		// TODO Auto-generated method stub
		return acIdentityMapperMd5Mapper.insertSelective(acIdentityMapperMd5);
	}

}
